using System.Text.Json.Serialization;

namespace MVCWeekOne.Models
{
    public class ApiResult<T>
    {
        [JsonPropertyName("msg")]
        public string? Msg { get; set; }

        [JsonPropertyName("code")]
        public int? Code { get; set; }

        [JsonPropertyName("data")]
        public T? Data { get; set; }

        // 添加一个属性来判断是否成功
        public bool IsSuccess => Code == 200 || Code == 0; // 200表示成功，0也表示成功
    }

    public class ApiPageing<T>
    {
        [JsonPropertyName("msg")]
        public string? Msg { get; set; }

        [JsonPropertyName("code")]
        public int Code { get; set; }

        [JsonPropertyName("totaCount")]
        public int TotaCount { get; set; }

        [JsonPropertyName("totaPage")]
        public int TotaPage { get; set; }

        [JsonPropertyName("pageData")]
        public IList<T>? PageData { get; set; }

        // 为了兼容性，添加Items属性
        public List<T> Items => PageData?.ToList() ?? new List<T>();
    }

    public class AccountDTO
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("name")]
        public string Name { get; set; } = string.Empty;

        [JsonPropertyName("pwd")]
        public string Pwd { get; set; } = string.Empty;

        [JsonPropertyName("realName")]
        public string RealName { get; set; } = string.Empty;

        [JsonPropertyName("isState")]
        public bool IsState { get; set; }

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("roleIds")]
        public List<long> RoleIds { get; set; } = new List<long>();

        [JsonPropertyName("roleName")]
        public List<string> RoleName { get; set; } = new List<string>();
    }

    public class AccountLoginDTO
    {
        [JsonPropertyName("accMsg")]
        public AccountMsg accMsg { get; set; } = new AccountMsg();

        [JsonPropertyName("toKen")]
        public string ToKen { get; set; } = string.Empty;
    }

    public class AccountMsg
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("name")]
        public string Name { get; set; } = string.Empty;

        [JsonPropertyName("realName")]
        public string RealName { get; set; } = string.Empty;

        [JsonPropertyName("isState")]
        public bool IsState { get; set; }

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("roleIds")]
        public List<long> RoleIds { get; set; } = new List<long>();

        [JsonPropertyName("roleName")]
        public List<string> RoleName { get; set; } = new List<string>();
    }

    // 工序管理相关模型
    public class ProcessQueryModel
    {
        [JsonPropertyName("pageNumber")]
        public int PageNumber { get; set; } = 1;

        [JsonPropertyName("pageSize")]
        public int PageSize { get; set; } = 30;

        [JsonPropertyName("processNumber")]
        public string ProcessNumber { get; set; } = string.Empty;

        [JsonPropertyName("processName")]
        public string ProcessName { get; set; } = string.Empty;

        [JsonPropertyName("reportingPermissions")]
        public string ReportingPermissions { get; set; } = string.Empty;

        [JsonPropertyName("reportingRatioStart")]
        public int? ReportingRatioStart { get; set; }

        [JsonPropertyName("reportingRatioEnd")]
        public int? ReportingRatioEnd { get; set; }

        [JsonPropertyName("defectiveItemList")]
        public string DefectiveItemList { get; set; } = string.Empty;

        [JsonPropertyName("testField1")]
        public string TestField1 { get; set; } = string.Empty;

        [JsonPropertyName("testField2")]
        public string TestField2 { get; set; } = string.Empty;

        [JsonPropertyName("creationTimeStart")]
        public DateTime? CreationTimeStart { get; set; }

        [JsonPropertyName("creationTimeEnd")]
        public DateTime? CreationTimeEnd { get; set; }

        [JsonPropertyName("sortField")]
        public string SortField { get; set; } = string.Empty;

        [JsonPropertyName("sortDirection")]
        public string SortDirection { get; set; } = string.Empty;
    }

    public class ProcessPageResult
    {
        [JsonPropertyName("totalCount")]
        public int TotalCount { get; set; }

        [JsonPropertyName("data")]
        public List<ProcessDto> Data { get; set; } = new List<ProcessDto>();

        [JsonPropertyName("pageNumber")]
        public int PageNumber { get; set; }

        [JsonPropertyName("pageSize")]
        public int PageSize { get; set; }

        [JsonPropertyName("totalPages")]
        public int TotalPages { get; set; }
    }

    public class ProcessDto
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("processNumber")]
        public string ProcessNumber { get; set; } = string.Empty;

        [JsonPropertyName("processName")]
        public string ProcessName { get; set; } = string.Empty;

        [JsonPropertyName("reportingPermissions")]
        public string ReportingPermissions { get; set; } = string.Empty;

        [JsonPropertyName("reportingRatio")]
        public int ReportingRatio { get; set; }

        [JsonPropertyName("defectiveItemList")]
        public string DefectiveItemList { get; set; } = string.Empty;

        [JsonPropertyName("testField1")]
        public string TestField1 { get; set; } = string.Empty;

        [JsonPropertyName("testField2")]
        public string TestField2 { get; set; } = string.Empty;

        [JsonPropertyName("creatorDate")]
        public DateTime CreatorDate { get; set; }

        [JsonPropertyName("creator")]
        public string Creator { get; set; } = string.Empty;

        [JsonPropertyName("updatorDate")]
        public DateTime? UpdatorDate { get; set; }

        [JsonPropertyName("updator")]
        public string Updator { get; set; } = string.Empty;
    }

    public class ProcessCollectionDataDto
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("processId")]
        public long ProcessId { get; set; }

        [JsonPropertyName("dataType")]
        public string DataType { get; set; } = string.Empty;

        [JsonPropertyName("dataName")]
        public string DataName { get; set; } = string.Empty;

        [JsonPropertyName("creatorDate")]
        public DateTime CreatorDate { get; set; }

        [JsonPropertyName("creator")]
        public string Creator { get; set; } = string.Empty;

        [JsonPropertyName("updatorDate")]
        public DateTime? UpdatorDate { get; set; }

        [JsonPropertyName("updator")]
        public string Updator { get; set; } = string.Empty;
    }

    // 为避免与 MVCWeekOne/Models/DefectiveModels.cs 中的 DefectiveDto 重名，
    // 这里使用一个更简单的DTO名称。
    public class DefectiveSimpleDto
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("defectiveCode")]
        public string DefectiveCode { get; set; } = string.Empty;

        [JsonPropertyName("defectiveName")]
        public string DefectiveName { get; set; } = string.Empty;

        [JsonPropertyName("attachmentPath")]
        public string AttachmentPath { get; set; } = string.Empty;

        [JsonPropertyName("imagePath")]
        public string ImagePath { get; set; } = string.Empty;

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("creatorId")]
        public long CreatorId { get; set; }

        [JsonPropertyName("modifierId")]
        public long? ModifierId { get; set; }

        [JsonPropertyName("modifyTime")]
        public DateTime? ModifyTime { get; set; }
    }

    public class RoleModel
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("isDeleted")]
        public bool IsDeleted { get; set; }

        [JsonPropertyName("roleName")]
        public string RoleName { get; set; } = string.Empty;

        [JsonPropertyName("isState")]
        public bool IsState { get; set; }

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }
    }

    public class RoleDto
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("roleName")]
        public string RoleName { get; set; } = string.Empty;

        [JsonPropertyName("isState")]
        public bool IsState { get; set; }

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("permissionNames")]
        public List<string> PermissionNames { get; set; } = new List<string>();

        [JsonPropertyName("permissionIds")]
        public List<long> PermissionIds { get; set; } = new List<long>();
    }

    public class PermissionModel
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("permissionName")]
        public string PermissionName { get; set; } = string.Empty;

        [JsonPropertyName("permissionURL")]
        public string PermissionURL { get; set; } = string.Empty;

        [JsonPropertyName("parentId")]
        public int ParentId { get; set; }

        [JsonPropertyName("parentName")]
        public string ParentName { get; set; } = string.Empty;

        [JsonPropertyName("isState")]
        public bool IsState { get; set; }

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("children")]
        public List<PermissionModel> Children { get; set; } = new List<PermissionModel>();
    }

    /// <summary>
    /// 工艺路线DTO
    /// </summary>
    public class RouteDto
    {
        [JsonPropertyName("id")]
        public int RouteId { get; set; }

        [JsonPropertyName("code")]
        public string RouteCode { get; set; } = string.Empty;

        [JsonPropertyName("name")]
        public string RouteName { get; set; } = string.Empty;

        [JsonPropertyName("enableApproval")]
        public bool IsEnabled { get; set; }

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("createBy")]
        public string CreateUser { get; set; } = string.Empty;

        [JsonPropertyName("updateTime")]
        public DateTime? UpdateTime { get; set; }

        [JsonPropertyName("updateBy")]
        public string UpdateUser { get; set; } = string.Empty;
    }

    /// <summary>
    /// 工艺路线工序DTO
    /// </summary>
    public class RouteProcessDto
    {
        [JsonPropertyName("processId")]
        public int ProcessId { get; set; }

        [JsonPropertyName("routeId")]
        public int RouteId { get; set; }

        [JsonPropertyName("processName")]
        public string ProcessName { get; set; } = string.Empty;

        [JsonPropertyName("processType")]
        public string ProcessType { get; set; } = string.Empty;

        [JsonPropertyName("sequence")]
        public int Sequence { get; set; }

        [JsonPropertyName("standardTime")]
        public decimal StandardTime { get; set; }

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("createUser")]
        public string CreateUser { get; set; } = string.Empty;

        [JsonPropertyName("updateTime")]
        public DateTime? UpdateTime { get; set; }

        [JsonPropertyName("updateUser")]
        public string UpdateUser { get; set; } = string.Empty;
    }

    /// <summary>
    /// 工艺路线详情DTO（包含工序列表）
    /// </summary>
    public class RouteDetailDto
    {
        [JsonPropertyName("route")]
        public RouteDto Route { get; set; } = new RouteDto();

        [JsonPropertyName("processes")]
        public List<RouteProcessDto> Processes { get; set; } = new List<RouteProcessDto>();
    }

    /// <summary>
    /// 生产计划DTO
    /// </summary>
    public class PlanDto
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        // JSON 返回字段为 "code"，此处直接映射
        [JsonPropertyName("code")]
        public string Code { get; set; } = string.Empty;

        // 向后兼容，提供 PlanCode 只读别名
        public string PlanCode => Code;

        [JsonPropertyName("remark")]
        public string Remark { get; set; } = string.Empty;

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("createBy")]
        public string CreateBy { get; set; } = string.Empty;

        [JsonPropertyName("updateTime")]
        public DateTime? UpdateTime { get; set; }

        [JsonPropertyName("updateBy")]
        public string UpdateBy { get; set; } = string.Empty;

        [JsonPropertyName("itemCount")]
        public int ItemCount { get; set; }
    }

    /// <summary>
    /// 生产计划明细DTO
    /// </summary>
    public class PlanItemDto
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("planId")]
        public int PlanId { get; set; }

        [JsonPropertyName("productId")]
        public int ProductId { get; set; }

        [JsonPropertyName("productCode")]
        public string ProductCode { get; set; } = string.Empty;

        [JsonPropertyName("productName")]
        public string ProductName { get; set; } = string.Empty;

        // 规格：支持 specification / spec
        private string _specification = string.Empty;
        [JsonPropertyName("specification")]
        public string Specification
        {
            get => _specification;
            set => _specification = value ?? string.Empty;
        }
        [JsonPropertyName("spec")]
        public string Spec
        {
            get => _specification;
            set => _specification = value ?? string.Empty;
        }

        [JsonPropertyName("unit")]
        public string Unit { get; set; } = string.Empty;

        // 数量：支持 plannedQuantity / quantity / qty
        private decimal _plannedQuantity;
        [JsonPropertyName("plannedQuantity")]
        public decimal PlannedQuantity
        {
            get => _plannedQuantity;
            set => _plannedQuantity = value;
        }
        [JsonPropertyName("quantity")]
        public decimal Quantity
        {
            get => _plannedQuantity;
            set => _plannedQuantity = value;
        }
        [JsonPropertyName("qty")]
        public decimal Qty
        {
            get => _plannedQuantity;
            set => _plannedQuantity = value;
        }

        // 完成数量：支持 completedQuantity / doneQuantity / doneQty
        private decimal _completedQuantity;
        [JsonPropertyName("completedQuantity")]
        public decimal CompletedQuantity
        {
            get => _completedQuantity;
            set => _completedQuantity = value;
        }
        [JsonPropertyName("doneQuantity")]
        public decimal DoneQuantity
        {
            get => _completedQuantity;
            set => _completedQuantity = value;
        }
        [JsonPropertyName("doneQty")]
        public decimal DoneQty
        {
            get => _completedQuantity;
            set => _completedQuantity = value;
        }

        // 工单编号：支持 workOrderNumber / workOrderCode
        private string _workOrderNumber = string.Empty;
        [JsonPropertyName("workOrderNumber")]
        public string WorkOrderNumber
        {
            get => _workOrderNumber;
            set => _workOrderNumber = value ?? string.Empty;
        }
        [JsonPropertyName("workOrderCode")]
        public string WorkOrderCode
        {
            get => _workOrderNumber;
            set => _workOrderNumber = value ?? string.Empty;
        }

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("createBy")]
        public string CreateBy { get; set; } = string.Empty;

        [JsonPropertyName("updateTime")]
        public DateTime? UpdateTime { get; set; }

        [JsonPropertyName("updateBy")]
        public string UpdateBy { get; set; } = string.Empty;
    }

    /// <summary>
    /// 生产计划详情DTO（包含明细列表）
    /// </summary>
    public class PlanDetailDto
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("code")]
        public string Code { get; set; } = string.Empty;

        [JsonPropertyName("remark")]
        public string Remark { get; set; } = string.Empty;

        [JsonPropertyName("createBy")]
        public string CreateBy { get; set; } = string.Empty;

        [JsonPropertyName("createTime")]
        public DateTime CreateTime { get; set; }

        [JsonPropertyName("updateBy")]
        public string UpdateBy { get; set; } = string.Empty;

        [JsonPropertyName("updateTime")]
        public DateTime? UpdateTime { get; set; }

        [JsonPropertyName("items")]
        public List<PlanProductDto> Items { get; set; } = new List<PlanProductDto>();
    }

    /// <summary>
    /// 计划关联产品DTO（与后端API保持一致）
    /// </summary>
    public class PlanProductDto
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("planId")]
        public int? PlanId { get; set; }

        [JsonPropertyName("productCode")]
        public string ProductCode { get; set; } = string.Empty;

        [JsonPropertyName("productName")]
        public string ProductName { get; set; } = string.Empty;

        [JsonPropertyName("spec")]
        public string? Spec { get; set; }

        [JsonPropertyName("unit")]
        public string? Unit { get; set; }

        [JsonPropertyName("producer")]
        public string? Producer { get; set; }

        [JsonPropertyName("productionStartTime")]
        public DateTime ProductionStartTime { get; set; }

        [JsonPropertyName("productionEndTime")]
        public DateTime ProductionEndTime { get; set; }

        [JsonPropertyName("createTime")]
        public DateTime? CreateTime { get; set; }

        [JsonPropertyName("updateTime")]
        public DateTime? UpdateTime { get; set; }
    }

    /// <summary>
    /// 产品选择DTO
    /// </summary>
    public class ProductSelectDto
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        // 产品编号：支持 productCode / code
        private string _productCode = string.Empty;
        [JsonPropertyName("productCode")]
        public string ProductCode
        {
            get => _productCode;
            set => _productCode = value ?? string.Empty;
        }
        [JsonPropertyName("code")]
        public string Code
        {
            get => _productCode;
            set => _productCode = value ?? string.Empty;
        }

        // 产品名称：支持 productName / name
        private string _productName = string.Empty;
        [JsonPropertyName("productName")]
        public string ProductName
        {
            get => _productName;
            set => _productName = value ?? string.Empty;
        }
        [JsonPropertyName("name")]
        public string Name
        {
            get => _productName;
            set => _productName = value ?? string.Empty;
        }

        // 规格：支持 specification / spec
        private string _specification = string.Empty;
        [JsonPropertyName("specification")]
        public string Specification
        {
            get => _specification;
            set => _specification = value ?? string.Empty;
        }
        [JsonPropertyName("spec")]
        public string Spec
        {
            get => _specification;
            set => _specification = value ?? string.Empty;
        }

        [JsonPropertyName("unit")]
        public string Unit { get; set; } = string.Empty;

        [JsonPropertyName("inventory")]
        public decimal Inventory { get; set; }

        // 库位：支持 storageLocation / location
        private string _storageLocation = string.Empty;
        [JsonPropertyName("storageLocation")]
        public string StorageLocation
        {
            get => _storageLocation;
            set => _storageLocation = value ?? string.Empty;
        }
        [JsonPropertyName("location")]
        public string Location
        {
            get => _storageLocation;
            set => _storageLocation = value ?? string.Empty;
        }
    }

    /// <summary>
    /// 批量删除生产计划结果
    /// </summary>
    public class BatchDeletePlanResult
    {
        [JsonPropertyName("successCount")]
        public int SuccessCount { get; set; }

        [JsonPropertyName("failCount")]
        public int FailCount { get; set; }

        [JsonPropertyName("failMessages")]
        public List<string> FailMessages { get; set; } = new List<string>();
    }

    /// <summary>
    /// 导入生产计划结果
    /// </summary>
    public class ImportPlanResult
    {
        [JsonPropertyName("successCount")]
        public int SuccessCount { get; set; }

        [JsonPropertyName("failCount")]
        public int FailCount { get; set; }

        [JsonPropertyName("failMessages")]
        public List<string> FailMessages { get; set; } = new List<string>();

        [JsonPropertyName("totalCount")]
        public int TotalCount { get; set; }
    }

    // Dashboard 相关的 DTO 类
    public class CumulativeDataDto
    {
        [JsonPropertyName("cumulativeUnitsData")]
        public int CumulativeUnitsData { get; set; }

        [JsonPropertyName("productionPlanData")]
        public int ProductionPlanData { get; set; }

        [JsonPropertyName("cumulativeQualifiedData")]
        public int CumulativeQualifiedData { get; set; }

        [JsonPropertyName("cumulativeUnqualifiedData")]
        public int CumulativeUnqualifiedData { get; set; }
    }

    public class DefectiveRateDto
    {
        [JsonPropertyName("totalDefective")]
        public int TotalDefective { get; set; }

        [JsonPropertyName("totalProduced")]
        public int TotalProduced { get; set; }

        [JsonPropertyName("defectiveRate")]
        public decimal DefectiveRate { get; set; }

        [JsonPropertyName("dateRange")]
        public string DateRange { get; set; } = string.Empty;
    }

    public class DefectiveItemRankDto
    {
        [JsonPropertyName("defectiveItemCode")]
        public string DefectiveItemCode { get; set; } = string.Empty;

        [JsonPropertyName("defectiveItemName")]
        public string DefectiveItemName { get; set; } = string.Empty;

        [JsonPropertyName("totalQuantity")]
        public int TotalQuantity { get; set; }

        [JsonPropertyName("percentage")]
        public decimal Percentage { get; set; }
    }

    public class ProductionStatisticsPageResult
    {
        [JsonPropertyName("data")]
        public List<ProductionStatisticsDto> Data { get; set; } = new List<ProductionStatisticsDto>();

        [JsonPropertyName("total")]
        public int Total { get; set; }
    }

    public class ProductionStatisticsDto
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("reportingDate")]
        public DateTime ReportingDate { get; set; }

        [JsonPropertyName("workOrderPlanCount")]
        public int? WorkOrderPlanCount { get; set; }

        [JsonPropertyName("productName")]
        public string ProductName { get; set; } = string.Empty;

        [JsonPropertyName("productNumber")]
        public string ProductNumber { get; set; } = string.Empty;

        [JsonPropertyName("quantity")]
        public int Quantity { get; set; }
    }

    public class WorkOrderComprehensivePageResult
    {
        [JsonPropertyName("data")]
        public List<WorkOrderComprehensiveInfoDto> Data { get; set; } = new List<WorkOrderComprehensiveInfoDto>();

        [JsonPropertyName("total")]
        public int Total { get; set; }
    }

    public class WorkOrderComprehensiveInfoDto
    {
        [JsonPropertyName("workOrder")]
        public WorkOrderDetailDto WorkOrder { get; set; } = new WorkOrderDetailDto();

        [JsonPropertyName("productInfo")]
        public ProductInfoDto ProductInfo { get; set; } = new ProductInfoDto();
    }

    public class WorkOrderDetailDto
    {
        [JsonPropertyName("code")]
        public string Code { get; set; } = string.Empty;

        [JsonPropertyName("plannedQuantity")]
        public int? PlannedQuantity { get; set; }

        [JsonPropertyName("actualQuantity")]
        public int? ActualQuantity { get; set; }
    }

    public class ProductInfoDto
    {
        [JsonPropertyName("productName")]
        public string ProductName { get; set; } = string.Empty;
    }
}
